{
  pkgs,
  lib,
  config,
  ...
}:
with lib; let
  cfg = config.modules.editor.kakoune;
in {
  options.modules.editor.kakoune = {enable = mkEnableOption "Kakoune";};
  config = mkIf cfg.enable {
    home.packages = with pkgs; [kakoune-cr kak-lsp];
    programs.kakoune = {
      enable = true;
      plugins = with pkgs; [
        kak-lsp
        kakounePlugins.active-window-kak
        kakounePlugins.prelude-kak
        kakounePlugins.connect-kak
        kakounePlugins.kakoune-buffers
        kakounePlugins.kakboard
        kakounePlugins.pandoc-kak
      ];
      config = {
        # colorScheme = "gotham";
        autoReload = "ask";

        alignWithTabs = false;
        tabStop = 2;
        indentWidth = 2;

        showMatching = true;
        showWhitespace.enable = false;

        scrollOff.lines = 8;
        scrollOff.columns = 3;

        numberLines = {
          enable = false;
          relative = true;
          # ⋮ ⦙ ⋅ ∘ ⌋ ∣
          separator = ''" "'';
        };

        ui = {
          enableMouse = true;
          # assistant = "none";
          statusLine = "bottom";
        };

        wrapLines = {
          enable = true;
          indent = true;
          maxWidth = 100;
          marker = "⎁";
        };

        hooks = [
          # kak-lsp
          {
            commands = "lsp-enable-window";
            name = "WinSetOption";
            option = "filetype=(sh|javascript|typescript|lua|nix)";
          }
          # auto-pairs
          {
            commands = "enable-auto-pairs";
            name = "WinCreate";
            option = ".*";
          }
          # nix
          {
            commands = ''
              set-option buffer formatcmd alejandra
            '';
            name = "WinCreate";
            option = ".*.nix";
          }
          {
            commands = "format";
            name = "BufWritePre";
            option = ".*.nix";
          }
          # kakboard
          {
            commands = "kakboard-enable";
            name = "WinCreate";
            option = ".*";
          }
        ];

        keyMappings = [
          {
            mode = "user";
            key = "l";
            effect = ": enter-user-mode lsp<ret>";
            docstring = "lsp";
          }
        ];
      };

      extraConfig = ''
        # Plug
        # ────────────────────────────────────────────────────
        evaluate-commands %sh{
        	plugins="$kak_config/plugins"
          mkdir -p "$plugins"
          [ ! -e "$plugins/plug.kak" ] && \
          git clone -q https://github.com/andreyorst/plug.kak.git "$plugins/plug.kak"
          printf "%s\n" "source '$plugins/plug.kak/rc/plug.kak'"
        }
        plug "andreyorst/plug.kak" noload

        # Loads & Sources
        # ────────────────────────────────────────────────────
        source "%val{config}/kakrc.local"
        require-module prelude
        require-module connect

        # Default Options
        # ────────────────────────────────────────────────────
        set-option global makecmd 'make -j 8'
        set-option global grepcmd 'rg --column'
        set-option global ui_options terminal_assistant=none

        # LSP Server
        # ────────────────────────────────────────────────────
        try %sh{ kak-lsp --kakoune -s $kak_session }

        # Buffers
        # ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
        try %{
          map global normal ^ q
          map global normal <a-^> Q
          map global normal q b
          map global normal Q B
          map global normal <a-q> <a-b>
          map global normal <a-Q> <a-B>
          map global normal b ': enter-buffers-mode<ret>' -docstring 'buffers'
          map global normal B ': enter-user-mode -lock buffers<ret>' -docstring 'buffers (lock)'
        }

        # Fuzzy-Finder
        # ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
        plug "https://github.com/andreyorst/fzf.kak" config %{
          map global normal <c-p> ': fzf-mode<ret>' -docstring "fzf mode"
        }

        # Auto-Pairs
        # ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
        plug "https://github.com/alexherbo2/auto-pairs.kak"

      '';
    };

    programs.bash = {
      shellAliases = {
        k = "kcr edit";
        K = "kcr-fzf-shell";
        KK = "K --working-directory .";
        ks = "kcr shell --session";
        kl = "kcr list";
        a = "kcr attach";
        ":f" = "kcr fzf files";
        "f:" = "KK kcr fzf files";
        ":g" = "kcr fzf grep";
        "g:" = "KK kcr fzf grep";
      };
      # profileExtra = {};
    };
  };
}
